<template>
  <div class="meetingDetail wboxnoborder" id="meetingDetail">
    <el-form ref="settingForm" :model="settingForm" label-width="80px" >
      <el-checkbox-group v-model="checkList">
        <el-form-item label="司长" prop="userId">
          <el-checkbox v-for="(item,index) in userlist.filter(item=>item.roleName==='ROLE_sizhang')" :key="index" :label="item.userId">
            {{item.realName}}
          </el-checkbox>
        </el-form-item>
        <el-form-item label="副司长" prop="userId">
          <el-checkbox v-for="(item,index) in userlist.filter(item=>item.roleName==='ROLE_fusizhang')" :key="index" :label="item.userId">
            {{item.realName}}
          </el-checkbox>
        </el-form-item>
        <el-form-item label="处长" prop="userId">
          <el-checkbox v-for="(item,index) in userlist.filter(item=>item.roleName==='ROLE_chuzhang')" :key="index" :label="item.userId">
            {{item.realName}}
          </el-checkbox>
        </el-form-item>
        <el-form-item label="处内人员" prop="userId">
          <el-checkbox v-for="(item,index) in userlist.filter(item=>item.roleName==='ROLE_banshirenyuan')" :key="index" :label="item.userId">
            {{item.realName}}
          </el-checkbox>
        </el-form-item>
      </el-checkbox-group>
      <el-form-item>
        <el-button type="success" @click="savesetting" :loading="saveLoading">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'settinguser',
  props: ['timestamp'],
  watch: {
    timestamp () {
      this.$refs['settingForm'].resetFields()
      this.loaduserlist()
    }
  },
  data () {
    return {
      settingFormRoles: {
        userId: [{ required: true, message: '请选择初审用户', trigger: 'blur' }]
      },
      settingForm: {
        userId: ''
      },
      userlist: [],
      saveLoading: false,
      deptCode: null,
      checkList: []
    }
  },
  methods: {
    savesetting () {
      this.$refs['settingForm'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          let para = {
            allUserIds: this.userlist.map(item => item.userId),
            userIds: this.checkList
          }
          this.$http
            .post(this.$http.defaults.youngAPI + `api/young/security/saveSpecialUsers`, para)
            .then(response => {
              if (response.data.success) {
                this.$message({
                  message: '设置成功',
                  type: 'success'
                })
                this.$emit('closeDialog')
              } else {
                this.$message({
                  message: '设置失败:' + response.data.message,
                  type: 'error'
                })
              }
              this.saveLoading = false
            })
            .catch(error => {
              console.log(error)
              this.$message({
                message: '网络请求失败:' + error,
                type: 'error'
              })
              this.saveLoading = false
            })
        }
      })
    },
    loaduserlist () {
      this.$http
        .get(this.$http.defaults.youngAPI + `api/young/security/getSpecialUsers`)
        .then(response => {
          if (response.data.success) {
            this.userlist = response.data.result
            this.checkList = this.userlist.filter(item => item.roleYoung).map(item => item.userId)
          } else {
            this.$message({
              message: '读取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '网络读取失败:' + error,
            type: 'error'
          })
        })
    }
  },
  mounted () {
    this.loaduserlist()
  }
}
</script>
